﻿var CurrentArea = {};
var CurrentEquipment = {};
Ext.onReady(function () {
    PageSafe();

    //人员列表
    var personstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.PersonManager.PersonView', 'GetPersonList'),
        fields: ['PID', 'UserName'],
        root: 'ResponseObject'
    });
    personstore.load();
    //人员列表
    var Modelstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'GetModelList'),
        fields: ['PID', 'ModelName'],
        root: 'ResponseObject'
    });
    Modelstore.load();
    //设备信息
    var equipmentstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'GetEquipmentList'),
        root: 'ResponseObject',
        fields: ['PID', 'EquipmentName', 'EquipmentCode', 'EquipmentIP', 'AreaID', 'PersonID', 'NTime', 'EquipmentChargePerson.UserName'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                re.params.ID = CurrentArea.id;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var equipmentsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = equipmentgrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentEquipment = k[0].json;
                }
            }
        }
    });
    var equipmentgrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 550,
        sm: equipmentsm,
        loadMask: true, //加载
        columns: [
       equipmentsm,
       { id: 'EquipmentName', header: '设备名称', width: 230, dataIndex: 'EquipmentName', sortable: true },
       { id: 'EquipmentCode', header: '设备ID', width: 100, dataIndex: 'EquipmentCode', sortable: true },
       { id: 'EquipmentIP', header: '设备IP', width: 200, dataIndex: 'EquipmentIP', sortable: true },
       { id: 'EquipmentChargePerson.UserName', header: '负责人', width: 200, dataIndex: 'EquipmentChargePerson.UserName', sortable: true },
       { id: 'NTime', header: '创建时间', width: 100, dataIndex: 'NTime', sortable: true }
   ],
        store: equipmentstore
    });
    ///////////////////////////////////////////////////////////////////////

    var AreaTree = new Ext.tree.TreePanel({
        width: 260,
        style: 'padding-left:20px',
        border: false,
        height: 530,
        split: true,
        rootVisible: true,
        useArrows: false, /*去掉树线*/
        autoScroll: true,
        collapseMode: 'mini',
        root: {},
        listeners: {
            render: function () {
                renderTree(AreaTree); /*渲染菜单树*/
            },
            checkchange: function (node, state) {
                if (state) {
                    CurrentArea = node.attributes;
                    equipmentstore.load();
                }
            }
        }
    });

    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'west',
            style: 'padding:2px;',
            title: '地区列表',
            iconCls: 'arealist-icon',
            items: AreaTree,
            width: 300
        }, {
            region: 'center',
            border: false,
            style: 'padding:2px;',
            iconCls: 'equipment-icon',
            tbar: [{ text: '增加设备', iconCls: 'add-icon', handler: function () {
                if (CurrentArea.id == null) {
                    Ext.Msg.alert("缺少项提示", "请选择要增加设备的地区");
                    return;
                }
                var equipmentaddwin = new Ext.Window({
                    height: 230,
                    width: 300,
                    title: '增加设备',
                    resizable: false,
                    iconCls: 'machine-icon',
                    modal: true,
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '所在地区',
                        width: 100,
                        readOnly: true,
                        id: 'E_Address',
                        name: 'E_Address'
                    }, {
                        fieldLabel: '负责人',
                        width: 100,
                        xtype: 'combo',
                        id: 'E_PersonName',
                        displayField: 'UserName',
                        valueField: 'PID',
                        selectOnFocus: true,
                        triggerAction: 'all',
                        store: personstore
                    }, {
                        fieldLabel: '设备名称',
                        width: 100,
                        id: 'E_Name',
                        name: 'E_Name'
                    }, {
                        fieldLabel: '设备识别码',
                        width: 150,
                        id: 'E_Code',
                        name: 'E_Code'
                    }, {
                        fieldLabel: '设备IP',
                        width: 150,
                        id: 'E_IP',
                        name: 'E_IP'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        var re = new JsonRequest();
                        re.params.equi = {
                            EquipmentName: Ext.getCmp("E_Name").getValue(),
                            EquipmentCode: Ext.getCmp("E_Code").getValue(),
                            EquipmentIP: Ext.getCmp("E_IP").getValue(),
                            AreaID: CurrentArea.id,
                            PersonID: Ext.getCmp("E_PersonName").getValue()
                        };
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'AddEquipment'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("操作提示", CurrentArea.text + "地区的" + obj.ResponseObject.EquipmentName + "设备增加成功");
                                    equipmentstore.load();
                                }
                                else {
                                    Ext.Msg.alert("操作提示", CurrentArea.text + "地区的" + obj.ResponseObject.EquipmentName + "设备增加失败，请联系管理员");
                                }
                                equipmentaddwin.close();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { equipmentaddwin.close(); } }],
                    listeners: {
                        render: function () {
                            if (CurrentArea.id != null) {
                                Ext.getCmp("E_Address").setValue(CurrentArea.text);
                            }
                        }
                    }
                }).show();
            }
            }, { text: '修改设备', iconCls: 'update-icon', handler: function () {
                if (CurrentEquipment.PID == null) {
                    Ext.Msg.alert("缺少项提示", "请选择需要修改的设备");
                    return;
                }
                var upwin = new Ext.Window({
                    height: 230,
                    width: 300,
                    title: '修改设备',
                    resizable: false,
                    iconCls: 'machine-icon',
                    modal: true,
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '所在地区',
                        width: 100,
                        readOnly: true,
                        id: 'u_Address',
                        name: 'u_Address'
                    }, {
                        fieldLabel: '负责人',
                        width: 100,
                        xtype: 'combo',
                        id: 'u_PersonName',
                        displayField: 'UserName',
                        valueField: 'PID',
                        selectOnFocus: true,
                        triggerAction: 'all',
                        store: personstore
                    }, {
                        fieldLabel: '设备名称',
                        width: 100,
                        id: 'u_Name',
                        name: 'u_Name'
                    }, {
                        fieldLabel: '设备识别码',
                        width: 150,
                        id: 'u_Code',
                        name: 'u_Code'
                    }, {
                        fieldLabel: '设备IP',
                        width: 150,
                        id: 'u_IP',
                        name: 'u_IP'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        var re = new JsonRequest();
                        re.params.equi = {
                            PID: CurrentEquipment.PID,
                            LastUpdate: CurrentEquipment.LastUpdate,
                            EquipmentName: Ext.getCmp("u_Name").getValue(),
                            EquipmentCode: Ext.getCmp("u_Code").getValue(),
                            EquipmentIP: Ext.getCmp("u_IP").getValue(),
                            AreaID: CurrentArea.id,
                            PersonID: Ext.getCmp("u_PersonName").getValue()
                        };
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'UpdateEquipment'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("操作提示", CurrentArea.text + "地区的" + obj.ResponseObject.EquipmentName + "设备修改成功");
                                    equipmentstore.load();
                                }
                                else {
                                    Ext.Msg.alert("操作提示", CurrentArea.text + "地区的" + obj.ResponseObject.EquipmentName + "设备修改失败，请联系管理员");
                                }
                                upwin.close();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { upwin.close(); } }],
                    listeners: {
                        render: function () {
                            if (CurrentArea.id != null) {
                                Ext.getCmp("u_Address").setValue(CurrentArea.text);
                                Ext.getCmp("u_PersonName").setValue(CurrentEquipment.PersonID);
                                Ext.getCmp("u_Name").setValue(CurrentEquipment.EquipmentName);
                                Ext.getCmp("u_Code").setValue(CurrentEquipment.EquipmentCode);
                                Ext.getCmp("u_IP").setValue(CurrentEquipment.EquipmentIP);
                            }
                        }
                    }
                }).show();
            }
            }, { text: '删除设备', iconCls: 'del-icon', handler: function () {
                if (CurrentEquipment.PID == null) {
                    Ext.Msg.alert("缺少项提示", "请选择需要删除的设备");
                    return;
                }
                var re = new JsonRequest();
                re.params.equi = CurrentEquipment;
                Ext.Ajax.request({
                    url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'DelEquipment'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (obj.ResponseObject != null) {
                            Ext.Msg.alert("操作提示", "设备删除成功");
                            equipmentstore.load();
                        }
                        else {
                            Ext.Msg.alert("操作提示", "设备删除失败，请联系管理员");
                        }
                    }
                });
            } 
            }],
            title: '设备管理',
            items: equipmentgrid
        }]
    });
});

//初始化树
var renderTree = function (tree) {
    var re = new JsonRequest();
    re.category = CATEGORY.READONLY;
    re.params.param = {};
    Ext.Ajax.request({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'AreaData'),
        jsonData: re,
        success: function (re, op) {
            var obj = Ext.decode(re.responseText).ResponseObject;
            var ns = {}, f = tree;
            ns.Children = obj;
            var r = new Ext.tree.TreeNode({
                id: '0',
                text: '全部地区'
            });
            var selectedNode = null, selectedId = f.selectedId;
            function appendChild(node, o) {
                if (node.PID == selectedId) {
                    selectedNode = node;
                }
                if (o.Children != null && o.Children.length > 0) {
                    for (var a = 0; a < o.Children.length; a++) {
                        var n = new Ext.tree.TreeNode({
                            id: o.Children[a].PID,
                            icon: '../images/menu/ui-text-area.png',
                            text: o.Children[a].AreaName,
                            parentId: o.Children[a].ParentID,
                            AreaRemark: o.Children[a].AreaRemark,
                            PersonID: o.Children[a].PersonID,
                            checked: false
                        });
                        node.appendChild(n);
                        appendChild(n, o.Children[a]);

                    }
                }
            }
            appendChild(r, ns);
            f.setRootNode(r);

            tree.registerNode(r); /*注册节点, 不然getNodeById(node.id)找不到这个节点*/
            tree.root.expand(true);
        }
    })
}